package com.zzjson.nacos.seata.store.service;

import com.zzjson.nacos.seata.store.mapper.ProductMapper;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>****************************************************************************</p>
 * <li>Description : TODO </li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2021年04月14日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
@Service
@Slf4j
public class ProductServiceImpl implements IProductService {

	@Autowired
	private ProductMapper productMapper;

	@Override
	public boolean reduceCount(Integer productId, Integer amount) {
		log.info("商品Id:{},商品数量:{}", productId, amount);
		log.info("当前 XID: {}", RootContext.getXID());
		// 检查库存
		try {
			checkStock(productId, amount);
		} catch (Exception e) {
			throw new RuntimeException();
		}
		log.info("开始扣减 {} 库存", productId);
		Integer record = productMapper.reduceCount(productId, amount);
		//Integer record = productMapper.reduceCountByBatch(Arrays.asList(1,2));
		//Integer record2 = productMapper.reduceCount(productId,amount);

		log.info("结束扣减 {} 库存结果:{}", productId, record > 0 ? "操作成功" : "扣减库存失败");
		return false;
	}


	private void checkStock(Integer productId, Integer requiredAmount) throws Exception {
		log.info("检查 {} 库存", productId);
		int countInDb = productMapper.selectCountById(productId);
		log.info("数据库库存:{}", countInDb);
		if (countInDb < requiredAmount) {
			log.warn("{} 库存不足，当前库存:{}", productId, countInDb);
			throw new Exception("库存不足");
		}
	}
}